package com.heima.aliyun.oss;

import com.aliyun.oss.OSS;
import com.heima.aliyun.oss.config.OssProperties;
import org.springframework.util.FileCopyUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * @author Administrator
 */
public class AliyunOssTemplate {
    private OssProperties ossProperties;
    private OSS ossClient;

    public AliyunOssTemplate(OssProperties ossProperties, OSS ossClient) {
        this.ossProperties = ossProperties;
        this.ossClient = ossClient;
    }

    /**
     * 上传文件
     *
     * @param filename 文件名称
     * @param is       文件流
     * @return
     */
    public String upload(String filename, InputStream is) throws IOException {
        String lastfilename = UUID.randomUUID() + filename.substring(filename.lastIndexOf("."));
        String storePath = new SimpleDateFormat("yyyy/MM/dd").format(new Date());
        //ossClient.putObject(ossProperties.getBucketName(), storePath, is);
        File file = new File("E:/images/" + storePath);
        if(!file.exists()){
            file.mkdirs();
        }
        FileCopyUtils.copy(is,new FileOutputStream("E:/images/"+storePath+"/"+lastfilename));
        return "http://localhost:88/" + storePath+"/"+lastfilename;
//        ossClient.putObject(ossProperties.getBucketName(), storePath, is);
//        return ossProperties.getReadPath() + storePath;
    }
}